package com.chinasofti.comparble;

public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        System.out.print("学生排序");
        return name + " " + age;
    }

    @Override
    public int compareTo(Student o) {
        //年龄从小到大
        int result = this.age - o.age;//结果为0则年龄相同
        //年龄相同比较名字首字母
        result = result == 0 ? this.name.compareTo(o.name) : result;
        return result;
    }
}
